package com.gkoudai.open.platform.common;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

/**
 * @author wangqi
 * @date 2025-02-17 11:07
 */
@Slf4j
@AllArgsConstructor
public enum TypeEnum {
    NEWS_CHANGE_LIST("queryDeleteOrUpdateList", null, "快讯变动接口"),
    CALENDAR("openData/calendar", null, "财经日历"),
    FUTURES_KLINE("futures/futuresQuotesKline", "FUTURESKLINE", "期货K线查询"),
    FUTURES_QUOTES("futures/futuresQuotes", "FUTUREQUOTES", "期货行情报价"),
    QUOTE_VOL("futures/quoteVol", "QUOTEVOL", "期货分笔"),
    REAL_TIME_QUOTE("futures/realTimeQuote", "REALTIMEQUOTE", "期货分价"),
    DELIVERY("futures/deliveryConfig", "DELIVERY", "期货公司合约交割日查询"),
    GOLD_NEWS_DETAIL("openData/importNewsContent", "IPTN", "黄金要闻详情"),
    GOLD_NEWS_LIST("openData/importNews", "IPTN", "黄金要闻列表"),
    KLINE("openData/kline", "KLINE", "K线数据"),
    NEWS_LIST("queryNewsList", null, "贵金属资讯"),
    STOCK_CASH_FLOW_STATEMENT("cashFlow", "CASHFLOW", "股票现金流量表"),
    STOCK_BALANCE_SHEET("balance", "BALANCE", "股票资产负债表"),
    STOCK_INCOME_STATEMENT("profit", "PROFIT", "股票利润表"),
    STOCK_FINANCIAL_METRICS("financialIndex", "FINANCIALINDEX", "股票财务指标"),
    STOCK_DAILY_METRICS("stockDailyIndex", "SDI", "股票每日指标"),
    STOCK_DAILY_QUOTES("stockDailyQuotes", "SDQ", "股票每日行情"),
    TOP_10_OUTSTANDING_SHAREHOLDERS("topTenFloatShareholder", "TTC", "十大流通股东"),
    TOP_10_SHAREHOLDERS("topTenShareholder", "TTS", "十大股东"),
    LIST_OF_STOCKS("stockList", "STOCKLIST", "股票列表"),
    PHYSICAL_GOLD_PRICE("physicalGoldPrice", "PHYSICALGOLDPRICE", "实物金价"),
    DEFERRED_DIRECTION("deferDirection", "DEFERDIRECTION", "递延方向"),
    ETF("etf", null, "etf"),
    QUOTES("quote", null, "行情报价"),
    INDICATORS_DETAIL_DATA("industryChain", null, "指标详情数据"),
    LIST_OF_BLACK_INDICATORS("blackSeriesInfo", "BSCI", "黑色系指标列表"),
    LIST_OF_INDICATORS_DEPARTMENT_CHEMICAL_ENGINEERING("chemicalIndustryInfo", "CECI", "化工系指标列表"),
    LIST_OF_INDICATORS_OF_THE_ENERGY_DEPARTMENT("energyInfo", "ECI", "能源系指标列表"),
    LIST_OF_AGRICULTURAL_INDICATORS("agriculturalProductsInfo", "APCI", "农产品指标列表"),
    BREED_CYCLE_RECOMMENDATION("futuresCycleRecommended", "VCR", "品种周期推荐"),
    FUTURE_DRAGON_QUOTE_LIST("futureList", "FVL", "龙虎榜期货品种列表"),
    FUTURE_DRAGON_LONG_SHORT_RATIO("multipleSpaceRatio", "MSR", "龙虎榜多空比"),
    FUTURE_DRAGON_LONG_SHORT_POSITION("longShortPosition", "DTBSP", "龙虎榜多空持仓"),
    FUTURE_DRAGON_NET_POSITION("netPosition", "DTNP", "龙虎榜净持仓"),
    ABNORMAL_POSITION("abnormalPosition", "ABNORMALPOSITION", "异动持仓"),
    HOT_LIST("popularList", "POPULARLIST", "热门榜单");

    private final String url;
    private final String type;
    private final String desc;
    public static final String KEY_NAME = "type";
    public static final String HOST="https://gopi.gkoudai.com/";

    public String getType() {
        return type;
    }

    public String getDesc() {
        return desc;
    }

    public String getUrl() {
        return HOST + this.url;
    }

    public static URI buildNewsChangeList(List<String> typeList, Integer page) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(NEWS_CHANGE_LIST.getUrl());
        for (String type : typeList) {
            setQueryParam(uriComponentsBuilder, KEY_NAME,type);
        }
        setQueryParam(uriComponentsBuilder, "page", page);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildCalendarUri(List<String> codeList, String date) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(CALENDAR.getUrl());
        for (String code : codeList) {
            setQueryParam(uriComponentsBuilder, KEY_NAME, code);
        }
        setQueryParam(uriComponentsBuilder, "date", date);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildRealTimeQuote(String code, String id, Integer size) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(REAL_TIME_QUOTE.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, REAL_TIME_QUOTE.getType());
        setQueryParam(uriComponentsBuilder, "code", code);
        setQueryParam(uriComponentsBuilder, "id", id);
        setQueryParam(uriComponentsBuilder, "size", size);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildFuturesQuotesUri(String typeCode, String code) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(FUTURES_QUOTES.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, FUTURES_QUOTES.getType());
        setQueryParam(uriComponentsBuilder, "code", code);
        setQueryParam(uriComponentsBuilder, "typeCode", typeCode);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildFuturesKlineUri(String code, Integer cycle, Integer count, Long ts) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(FUTURES_KLINE.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, FUTURES_KLINE.getType());
        setQueryParam(uriComponentsBuilder, "code", code);
        setQueryParam(uriComponentsBuilder, "cycle", cycle);
        setQueryParam(uriComponentsBuilder, "count", count);
        if (Objects.nonNull(ts)) {
            setQueryParam(uriComponentsBuilder, "ts", ts);
        } else {
            setQueryParam(uriComponentsBuilder, "ts", "");
        }
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildQuoteVolUri(String code) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(QUOTE_VOL.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, QUOTE_VOL.getType());
        setQueryParam(uriComponentsBuilder, "code", code);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildDeliveryConfigUri(String code) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(DELIVERY.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, DELIVERY.getType());
        setQueryParam(uriComponentsBuilder, "code", code);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildGoldNewsDetail(String id) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(GOLD_NEWS_DETAIL.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, GOLD_NEWS_DETAIL.getType());
        setQueryParam(uriComponentsBuilder, "id", id);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildGoldNewsList(Integer page, Integer size) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(GOLD_NEWS_LIST.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, GOLD_NEWS_LIST.getType());
        setQueryParam(uriComponentsBuilder, "page", page);
        setQueryParam(uriComponentsBuilder, "size", size);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildKlineUri(String code, Integer cycle, Integer count, Long ts) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(KLINE.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, KLINE.getType());
        setQueryParam(uriComponentsBuilder, "code", code);
        setQueryParam(uriComponentsBuilder, "cycle", cycle);
        setQueryParam(uriComponentsBuilder, "count", count);
        if (Objects.nonNull(ts)) {
            setQueryParam(uriComponentsBuilder, "ts", ts);
        } else {
            setQueryParam(uriComponentsBuilder, "ts", "");
        }
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildNewsListUri(String type, String pageNum, String pageSize) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(NEWS_LIST.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, type);
        setQueryParam(uriComponentsBuilder, "current", pageNum);
        setQueryParam(uriComponentsBuilder, "pageSize", pageSize);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildStockCashFlowStatementUri(String code) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(STOCK_CASH_FLOW_STATEMENT.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, STOCK_CASH_FLOW_STATEMENT.getType());
        setQueryParam(uriComponentsBuilder, "code", code);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildStockBalanceSheet(String code) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(STOCK_BALANCE_SHEET.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, STOCK_BALANCE_SHEET.getType());
        setQueryParam(uriComponentsBuilder, "code", code);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildStockIncomeStatementUri(String code) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(STOCK_INCOME_STATEMENT.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, STOCK_INCOME_STATEMENT.getType());
        setQueryParam(uriComponentsBuilder, "code", code);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildStockFinancialMetricsUri(String code) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(STOCK_FINANCIAL_METRICS.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, STOCK_FINANCIAL_METRICS.getType());
        setQueryParam(uriComponentsBuilder, "code", code);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildStockDailyMetricsUri(String code) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(STOCK_DAILY_METRICS.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, STOCK_DAILY_METRICS.getType());
        setQueryParam(uriComponentsBuilder, "code", code);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildStockDailyQuotesUri(String code) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(STOCK_DAILY_QUOTES.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, STOCK_DAILY_QUOTES.getType());
        setQueryParam(uriComponentsBuilder, "code", code);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildTop10OutstandingShareholdersUri(String code) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(TOP_10_OUTSTANDING_SHAREHOLDERS.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, TOP_10_OUTSTANDING_SHAREHOLDERS.getType());
        setQueryParam(uriComponentsBuilder, "code", code);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildTop10ShareholdersUri(String code) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(TOP_10_SHAREHOLDERS.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, TOP_10_SHAREHOLDERS.getType());
        setQueryParam(uriComponentsBuilder, "code", code);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildStockListUri(String pageNum, String pageSize) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(LIST_OF_STOCKS.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, LIST_OF_STOCKS.getType());
        setQueryParam(uriComponentsBuilder, "pageNum", pageNum);
        setQueryParam(uriComponentsBuilder, "pageSize", pageSize);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildPhysicalGoldPriceUri() {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(PHYSICAL_GOLD_PRICE.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, PHYSICAL_GOLD_PRICE.getType());
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildDeferredDirection() {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(DEFERRED_DIRECTION.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, DEFERRED_DIRECTION.getType());
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildEtfUri(String type) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(ETF.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, type);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildQuotesUri(String type, String code) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(QUOTES.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, type);
        setQueryParam(uriComponentsBuilder, "code", code);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildIndicatorsDetailDataUri(String type) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(INDICATORS_DETAIL_DATA.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, type);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildChemicalIndicatorsInfoUri(String category) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(LIST_OF_INDICATORS_DEPARTMENT_CHEMICAL_ENGINEERING.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, LIST_OF_INDICATORS_DEPARTMENT_CHEMICAL_ENGINEERING.getType());
        if (!StringUtils.isEmpty(category)) {
            setQueryParam(uriComponentsBuilder, "category", category);
        }
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildBlackIndicatorsInfoUri(String category) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(LIST_OF_BLACK_INDICATORS.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, LIST_OF_BLACK_INDICATORS.getType());
        if (!StringUtils.isEmpty(category)) {
            setQueryParam(uriComponentsBuilder, "category", category);
        }
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildEnergyIndicatorsInfoUri(String category) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(LIST_OF_INDICATORS_OF_THE_ENERGY_DEPARTMENT.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, LIST_OF_INDICATORS_OF_THE_ENERGY_DEPARTMENT.getType());
        if (!StringUtils.isEmpty(category)) {
            setQueryParam(uriComponentsBuilder, "category", category);
        }
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildAgriculturalProductsInfoUri(String category) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(LIST_OF_AGRICULTURAL_INDICATORS.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, LIST_OF_AGRICULTURAL_INDICATORS.getType());
        if (!StringUtils.isEmpty(category)) {
            setQueryParam(uriComponentsBuilder, "category", category);
        }
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildBreedCycleRecommendationUri(Integer month) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(BREED_CYCLE_RECOMMENDATION.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, BREED_CYCLE_RECOMMENDATION.getType());
        setQueryParam(uriComponentsBuilder, "month", month);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildFutureDragonQuoteListUri(Integer page, Integer size) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(FUTURE_DRAGON_QUOTE_LIST.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, FUTURE_DRAGON_QUOTE_LIST.getType());
        setQueryParam(uriComponentsBuilder, "pageNum", page);
        setQueryParam(uriComponentsBuilder, "pageSize", size);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildFutureDragonLongShortRatioUri(LocalDate tradeDate) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(FUTURE_DRAGON_LONG_SHORT_RATIO.getUrl());
        uriComponentsBuilder.queryParam(KEY_NAME, FUTURE_DRAGON_LONG_SHORT_RATIO.getType());
        uriComponentsBuilder.queryParam("tradeDate", tradeDate.format(DateTimeFormatter.ISO_LOCAL_DATE));
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildFutureDragonLongShortPosition(String quoteCode) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(FUTURE_DRAGON_LONG_SHORT_POSITION.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, FUTURE_DRAGON_LONG_SHORT_POSITION.getType());
        setQueryParam(uriComponentsBuilder, "quoteCode", quoteCode);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildFutureDragonNetPosition(String quoteCode) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(FUTURE_DRAGON_NET_POSITION.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, FUTURE_DRAGON_NET_POSITION.getType());
        setQueryParam(uriComponentsBuilder, "quoteCode", quoteCode);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildHotListUri(String categorize) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(HOT_LIST.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, HOT_LIST.getType());
        setQueryParam(uriComponentsBuilder, "categorize", categorize);
        return uriComponentsBuilder.build().toUri();
    }

    public static URI buildAbnormalPositionUri(LocalDate date, Integer page, Integer size) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(ABNORMAL_POSITION.getUrl());
        setQueryParam(uriComponentsBuilder, KEY_NAME, ABNORMAL_POSITION.getType());
        setQueryParam(uriComponentsBuilder, "pageNum", page);
        setQueryParam(uriComponentsBuilder, "pageSize", size);
        setQueryParam(uriComponentsBuilder, "date", date.format(DateTimeFormatter.ISO_LOCAL_DATE));
        return uriComponentsBuilder.build().toUri();
    }

    private static void setQueryParam(UriComponentsBuilder uriComponentsBuilder, String name, Object value) {
        if (Objects.nonNull(value)) {
            uriComponentsBuilder.queryParam(name, value);
        }
    }

}
